//
//  DeviceTool.m
//  MoDuo
//
//  Created by jemmy_wxh on 2017/9/8.
//  Copyright © 2017年 Jiang. All rights reserved.
//

#import "DeviceTool.h"

#import "sys/utsname.h"
#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

#define IOS_CELLULAR    @"pdp_ip0"
#define IOS_WIFI        @"en0"
//#define IOS_VPN       @"utun0"
#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"

@implementation DeviceTool

- (void)getDeviceInfo
{
    // 这个方法后面会列出来
    NSString *deviceName = [DeviceTool getDeviceName];
    NSLog(@"设备型号-->%@", deviceName);
    
    NSString *iPhoneName = [UIDevice currentDevice].name;
    NSLog(@"iPhone名称-->%@", iPhoneName);
    
    NSString *appVerion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"app版本号-->%@", appVerion);
    
    NSString *appSign = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleSignature"];
    NSLog(@"app签名信息-->%@",appSign);
    
    CGFloat batteryLevel = [[UIDevice currentDevice] batteryLevel];
    NSLog(@"电池电量-->%f", batteryLevel);
    
    NSString *localizedModel = [UIDevice currentDevice].localizedModel;
    NSLog(@"localizedModel-->%@", localizedModel);
    
    NSString *systemName = [UIDevice currentDevice].systemName;
    NSLog(@"当前系统名称-->%@", systemName);
    
    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    NSLog(@"当前系统版本号-->%@", systemVersion);
    
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSLog(@"唯一识别码uuid-->%@", uuid);
    
}
//获取设备惟一标示符
+ (NSString *)getUUID
{
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    return uuid;
}
//获取app版本号
+ (NSString *)getVersion
{
    NSString *appVerion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    return appVerion;
}
+ (NSString *)getMacAddress {
    int mib[6];
    size_t len;
    char *buf;
    unsigned char *ptr;
    struct if_msghdr *ifm;
    struct sockaddr_dl *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    
    NSString *outstring = [NSString stringWithFormat:@"%02x-%02x-%02x-%02x-%02x-%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    
    return [outstring uppercaseString];
}
//获取手机型号
+ (NSString *)getPhoneType
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *device_model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return device_model;
}
// 获取设备型号然后手动转化为对应名称
+ (NSString *)getDeviceName
{
    // 需要#import "sys/utsname.h"
#warning 题主呕心沥血总结！！最全面！亲测！全网独此一份！！
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"4";
    if ([deviceString isEqualToString:@"iPhone3,3"])    return @"4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"i5";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"5(GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"5c(GSM)";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"5c(GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"5s(GSM)";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"5s(GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"6P";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"6sp";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"SE";
    // 日行两款手机型号均为日本独占，可能使用索尼FeliCa支付方案而不是苹果支付
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"7";
    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"7P";
    if ([deviceString isEqualToString:@"iPhone9,3"])    return @"7U";
    if ([deviceString isEqualToString:@"iPhone9,4"])    return @"7P";
    if ([deviceString isEqualToString:@"iPhone10,1"])    return @"8";
    if ([deviceString isEqualToString:@"iPhone10,2"])    return @"8P";
    
    
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    
    return deviceString;
}

@end
